package me.pearjelly.pmc.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import me.pearjelly.pmc.dao.DocDAO;
import me.pearjelly.pmc.dao.PicGroupDAO;
import me.pearjelly.pmc.service.SimpleService;
import me.pearjelly.pmc.util.Logger;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class SimpleServiceImpl implements SimpleService{
	@Override
	public List<Document> getDocs() {
		return docDAO.selectNeedReIndex(maxRows);
	}
	@Override
	public List<Long> reIndex(List<Document> list) {
		File dir = new File(indexDir);
		Directory directory;
		try {
			directory = FSDirectory.open(dir);
			IndexReader indexReader = null;
			boolean update = Boolean.FALSE;
			if(IndexReader.indexExists(directory)){
				indexReader = IndexReader.open(directory,false);
				update = Boolean.TRUE;
				
			}
			List<Long> idList = new LinkedList<Long>();
			List<Document> tmpList = new LinkedList<Document>();
			for (Document document : list) {
				String idValue = document.get("id");
				if(null != idValue){
					idList.add(Long.parseLong(idValue));
					tmpList.add(document);
					if(update){
						//delete old document
						int count = indexReader.deleteDocuments(new Term("id", idValue));
						log.debug("Delete %d old document(s) id:%s", count, idValue);
					}
				}else{
					log.error("Get PicGroup id null from document:%s", document);
				}
			}
			if(update && indexReader!=null){
				indexReader.close();
			}
			IndexWriter indexWriter = new IndexWriter(directory, analyzer, MaxFieldLength.LIMITED);
			for (Document document : tmpList) {
				indexWriter.addDocument(document);
				log.debug("Index document:%s", document);				
			}
			indexWriter.optimize();
			indexWriter.close();
			log.info("(Re)Indexed %d documents.", tmpList.size());
			return idList;
		} catch (IOException e) {
			log.error("Access index dircetory %s error:%s. ", indexDir, e.getMessage());
			return null;
		}		
	}
	@Override
	public void updateDatas(List<Long> idList) {
		picGroupDAO.updateIndexFlag(idList);
	}
	private static final Logger log = new Logger();
	private DocDAO docDAO;
	private PicGroupDAO picGroupDAO;
	private int maxRows = 2000;
	private String indexDir = "/opt/pms/index/data";
	private Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_30);
	public void setIndexDir(String indexDir) {
		this.indexDir = indexDir;
	}	
	public void setMaxRows(int maxRows) {
		this.maxRows = maxRows;
	}
	public void setDocDAO(DocDAO docDAO) {
		this.docDAO = docDAO;
	}	
	public void setPicGroupDAO(PicGroupDAO picGroupDAO) {
		this.picGroupDAO = picGroupDAO;
	}
	public void setAnalyzer(Analyzer analyzer) {
		this.analyzer = analyzer;
	}	
}
